#ifndef _GATEWAY_CLIENT_H_
#define _GATEWAY_CLIENT_H_

#include "gateway_task.h"
#include "gateway_network_event.h"
#include "../mqttservice/mqttservice.h"
#include <vector>
#include <boost/enable_shared_from_this.hpp>

typedef enum{CMDCONNECT = 0, CMDSTATE, CMDPING, CMDEND}CMDTYPE;
typedef struct{
	std::string strcmddata;
	CMDTYPE cmdtype;
}CMDDATA;

class gateway_client : public gateway_stream_event, public IMqttEvent, public gateway_task,  public boost::enable_shared_from_this<gateway_task> {
	struct protohead{
		std::string srcval; //base64编码
		std::string ver;
		std::string strid; //
		std::string caddr;//集中器地址
		std::string cpcode;// 公司代码
		std::string ctrlcode; //数据方向控制码
		std::string fsn; //帧序列号
		std::string sdsn; //序列域
		std::string pwdver; //密钥版本
		std::string fcode; //功能码
	};
	
	struct firststat
	{
		unsigned char vstate:2;//阀门状态
		unsigned char ovva:1; //关阀电压告警
		unsigned char pmark:1; //提示标记
		unsigned char alarm:1; //报警状态
		unsigned char mstate:1; //余量状态
		unsigned char cct:1; //计数通道二
		unsigned char cco:1; //计数通道一
	};
	
	struct secondstat
	{
		unsigned char wmstatus:1; //无线模块状态
		unsigned char smisign:1; //强磁干扰标志
		unsigned char foffvmark:1; //强制关阀标记
		unsigned char cfsign:1; //电路故障标志
		unsigned char fopenvmark:1; //强制开阀标记
		unsigned char ovamark:1; //关阀报警标记
		unsigned char mmark:1; //余量标记
		unsigned char omark:1 ; //透支标记
	};
	
	struct threestat
	{
		unsigned char res:1; //预留
		unsigned char sfasign:1; //小流量异常标志
		unsigned char lfasign:1;//大流量异常标志
		unsigned char glmark:1; //燃气泄漏标志
		unsigned char dytbsign:1; //干黄管坏标志
		unsigned char pvalarm:1; //提示电压报警
		unsigned char csstatus:1; //插卡开关状态
		unsigned char getstatus:1;//查询状态
	};
	union threebyte
	{
		threestat ts;
		unsigned char byte;
	};
	
	union twobyte
	{
		secondstat ss;
		unsigned char byte;
	};
	
	union onebyte
	{
		firststat fs;
		unsigned char byte;
	};

	struct event_data{
		std::vector<std::string> vecjson;
	};
	
public:
	explicit gateway_client(gateway_stream_ptr stream_ptr);
	virtual ~gateway_client();

	/**** gateway_stream_event ****/

	//网络连接事件
	virtual int stream_init();

	//网络断开事件
	virtual int stream_clean();

	//网络读取事件
	virtual int stream_read(unsigned char* data, unsigned int dataLen);

	/**** gateway_task ****/
	virtual int on_task(void* param);

public:
    
	int OnConnect(CMQTTService *mq, std::string strid, int rc);
	int OnDisconnect(CMQTTService *mq, std::string strid, int rc);
	int OnMessage(CMQTTService *mq, std::string strid, ShadowActions actions, std::string payload);
	int OnSubscribe(CMQTTService *mq, std::string strid, int mid, int qos_count, const int *granted_qos);
    int OnUnsubscribe(CMQTTService *mq, std::string strid, int mid);
	int OnPublish(CMQTTService *mq, std::string strid, int mid);

public:
	void close();	
	std::string get_addr(){return _addr;}
private:

	int handle_parse_packet(unsigned char* buf, unsigned int bufLen, unsigned int &used);

	int handle_mqtt_event(std::vector<std::string> vecjson);

	int get_cmd_json(std::string strcmd, std::string &strjson);

	std::string get_dev_id(std::string strcmd);

	int get_mqtt_config(MQTTCONFIG & mqttconfig);

	int gateway_packet_ack();
	
	//boost::shared_ptr<gateway_client> get_ptr();

private:		
	std::string _host;
	bool _is_connected;
	unsigned short _port;
	std::string _addr;//ip:port
	//unsigned int _last_packet_id;
	unsigned int _ping_time;
	std::string _strdevid;
	std::string strkey;
	MQTTCONFIG _mqttconfig;
	gateway_stream_ptr _stream_ptr;
	//cmqtt_service_ptr _mqtt_ptr;
	CMQTTService *_mqtt;
	boost::shared_ptr<gateway_client> _client_ptr;
};

typedef boost::shared_ptr<gateway_client>gateway_client_ptr;
#endif 